<template>
  <div class="home">
    <el-container>
      <el-header height="auto" class="hero">
        <div class="hero-content">
          <h1 class="hero-title">
            <i class="fas fa-code"></i>
            探索科技与创新的前沿
          </h1>
          <p class="hero-subtitle">
            专注后端与云原生，分享工程实践与可观测性最佳实践，助你更快交付可靠系统
          </p>
          <div class="hero-stats">
            <div class="stat-item">
              <div class="stat-number">{{ metas.length }}+</div>
              <div class="stat-label">技术文章</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">{{ allTags.length }}+</div>
              <div class="stat-label">技术标签</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">100k+</div>
              <div class="stat-label">累计阅读</div>
            </div>
          </div>
          <div class="hero-actions">
            <RouterLink to="/blog" class="btn btn-primary">
              <i class="fas fa-rocket"></i>
              开始探索
              </RouterLink>
            <RouterLink to="/about" class="btn btn-outline">
              <i class="fas fa-user"></i>
                  关于我
              </RouterLink>
            </div>
        </div>
      </el-header>

      <el-main>
        <el-row :gutter="16" justify="center">
          <el-col :xs="24" :sm="20" :md="18" :lg="16">
            <el-card class="card" shadow="hover">
              <template #header>
                <div class="card-header">
                  <span class="card-title">
                    <i class="fas fa-rocket"></i>
                    快速入口
                  </span>
                </div>
              </template>
              <div class="quick">
                <RouterLink to="/blog">
                  <el-button type="primary" plain round>查看全部文章</el-button>
                </RouterLink>
                <el-button plain round @click="goRandom">随机一篇</el-button>
              </div>
            </el-card>

            <el-card class="card" shadow="hover" style="margin-top:16px;">
                            <template #header>
                <div class="card-header">
                  <span class="card-title">
                    <i class="fas fa-newspaper"></i>
                    最新文章
                  </span>
                  <RouterLink to="/blog"><el-link type="primary">更多</el-link></RouterLink>
                </div>
              </template>
              <el-skeleton :loading="loading" animated :count="1">
                <template #default>
                  <el-space fill direction="vertical" :size="8">
                    <RouterLink v-for="p in latest" :key="p.slug" :to="{ name: 'post', params: { slug: p.slug } }">
                      <el-card shadow="never" class="subcard">
                        {{ p.title }}
                      </el-card>
                    </RouterLink>
                  </el-space>
                </template>
              </el-skeleton>
            </el-card>

            <el-card class="card" shadow="hover" style="margin-top:16px;">
              <template #header>
                <div class="card-header">
                  <span class="card-title">
                    <i class="fas fa-chart-pie"></i>
                    可视化概览
                  </span>
                </div>
              </template>
              <el-row :gutter="16">
                <el-col :xs="24" :md="12">
                  <TagsPieChart :data="tagPieData" />
                </el-col>
                <el-col :xs="24" :md="12">
                  <PostsMonthlyLine :timeline="monthlyTimeline" />
                </el-col>
              </el-row>
            </el-card>

            <el-card class="card" shadow="hover" style="margin-top:16px;">
              <template #header>
                <div class="card-header">
                  <span class="card-title">
                    <i class="fas fa-info-circle"></i>
                    项目简介
                  </span>
                </div>
              </template>
              <p class="muted">文章内容来自仓库内的 <code>articles/*.md</code>，通过 Vite 动态导入并用 MarkdownIt 渲染。</p>
            </el-card>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
import { usePostsStore } from '@/stores/posts';
import TagsPieChart from '@/components/TagsPieChart.vue';
import PostsMonthlyLine from '@/components/PostsMonthlyLine.vue';
import { useRouter } from 'vue-router';

const store = usePostsStore();
const loading = ref(true);
const metas = computed(() => store.metas);
const latest = computed(() => metas.value.slice(0, 6));
const allTags = computed(() => store.allTags);
const router = useRouter();

async function goRandom() {
  if (!store.metas.length) {
    await store.loadMetas();
  }
  const list = store.metas;
  if (!list.length) return;
  const index = Math.floor(Math.random() * list.length);
  const slug = list[index].slug;
  router.push({ name: 'post', params: { slug } });
}

const tagPieData = computed(() => {
  const map = new Map<string, number>();
  for (const m of metas.value) {
    m.tags?.forEach(t => map.set(t, (map.get(t) || 0) + 1));
  }
  return Array.from(map.entries())
    .map(([name, value]) => ({ name, value }))
    .sort((a, b) => b.value - a.value)
    .slice(0, 20);
});

const monthlyTimeline = computed(() => {
  const map = new Map<string, number>();
  for (const m of metas.value) {
    if (!m.date) continue;
    const month = m.date.slice(0, 7); // 例如 2024-07
    map.set(month, (map.get(month) || 0) + 1);
  }
  const months = Array.from(map.keys()).sort();
  return months.map(month => ({ month, count: map.get(month) || 0 }));
});

onMounted(async () => {
  await store.loadMetas();
  loading.value = false;
});
</script>

<style scoped>
.hero { 
  padding: 150px 0 100px; 
  text-align: center;
  position: relative;
}

.hero-content {
  max-width: 1000px;
  margin: 0 auto;
  padding: 0 1.5rem;
}

.hero-title {
  font-family: 'Orbitron', monospace;
  font-size: 3.5rem;
  font-weight: 700;
  margin-bottom: 30px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: glow 2s ease-in-out infinite alternate;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
}

.hero-title i {
  color: var(--primary);
  animation: pulse 2s infinite;
}

.hero-subtitle {
  font-size: 1.3rem;
  color: var(--gray);
  margin-bottom: 40px;
  max-width: 700px;
  margin-left: auto;
  margin-right: auto;
  line-height: 1.6;
}

.hero-stats {
  display: flex;
  justify-content: center;
  gap: 60px;
  margin-bottom: 40px;
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-family: 'Orbitron', monospace;
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 8px;
}

.stat-label {
  color: var(--gray);
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.hero-actions {
  display: flex;
  gap: 20px;
  justify-content: center;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 15px 30px;
  border-radius: 50px;
  font-weight: 500;
  font-size: 1rem;
  text-decoration: none;
  transition: all 0.3s ease;
  cursor: pointer;
  border: 2px solid transparent;
}

.btn-primary {
  background: linear-gradient(45deg, var(--primary), var(--secondary));
  color: white;
  box-shadow: 0 4px 15px rgba(37, 99, 235, 0.3);
}

.btn-primary:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(37, 99, 235, 0.4);
}

.btn-outline {
  background: var(--glass);
  border-color: var(--glass-border);
  color: var(--light);
  backdrop-filter: blur(10px);
}

.btn-outline:hover {
  background: rgba(37, 99, 235, 0.1);
  border-color: var(--primary);
  transform: translateY(-3px);
}

.card { 
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(15px);
  transition: all 0.3s ease;
}

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

.card-header { 
  display: flex; 
  align-items: center; 
  justify-content: space-between; 
}

.card-title {
  font-family: 'Orbitron', monospace;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--light);
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-title i {
  color: var(--primary);
}

.muted {
  color: var(--gray) !important;
}

.quick { 
  display: flex; 
  gap: .75rem; 
}

.subcard { 
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
  width:1560px;
  color: #ffffff
}

.subcard:hover {
  background: rgba(37, 99, 235, 0.05);
  border-color: var(--primary);
  transform: translateX(5px);
}

@keyframes glow {
  from {
    text-shadow: 0 0 20px rgba(37, 99, 235, 0.5);
  }
  to {
    text-shadow: 0 0 30px rgba(37, 99, 235, 0.8), 0 0 40px rgba(139, 92, 246, 0.3);
  }
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
}

@media (max-width: 768px) {
  .hero-title {
    font-size: 2.5rem;
    flex-direction: column;
    gap: 10px;
  }
  
  .hero-subtitle {
    font-size: 1.1rem;
  }
  
  .hero-stats {
    gap: 30px;
  }
  
  .stat-number {
    font-size: 2rem;
  }
  
  .hero-actions {
    flex-direction: column;
    align-items: center;
  }
}

@media (max-width: 576px) {
  .hero {
    padding: 120px 0 80px;
  }
  
  .hero-title {
    font-size: 2rem;
  }
  
  .hero-stats {
    flex-direction: column;
    gap: 20px;
  }
}
</style>

